/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI.SavingBookManager;

import BLL.SavingBook_BLL;
import BLL.Unity;
import java.awt.event.ItemEvent;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mr.Sam
 */
public class SearchSavingBook extends javax.swing.JInternalFrame {

    Unity unity=new Unity();
    String sql = "select BookID,FullName,IDNumber,TypeName,Term,AccountBalance,OpeningDate,SBStatus\n"
            + "from (SAVINGBOOK inner join CUSTOMER on CUSTOMER.CustomerID=SAVINGBOOK.CustomerID) inner join SAVINGTYPE on SAVINGBOOK.TypeID=SAVINGTYPE.TypeID";
    SavingBook_BLL savingbook_bll = new SavingBook_BLL();
    TableSavingBook model = new TableSavingBook();
    int count=0;
    public SearchSavingBook() throws Exception {
        initComponents();
        LoaiSo.addItem("Co Ky Han");
        LoaiSo.addItem("Khong Ky Han");
        model.SetTable(Table);
        model.SetData(sql);
    }
    public void ReloadTable(String sql) throws Exception
    {
        model.RemoveData();
        model.SetData(sql);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jXPanel1 = new org.jdesktop.swingx.JXPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        Table = new org.jdesktop.swingx.JXTable();
        jLabel2 = new javax.swing.JLabel();
        MaSo = new org.jdesktop.swingx.JXTextField();
        jLabel3 = new javax.swing.JLabel();
        HoTen = new org.jdesktop.swingx.JXTextField();
        jLabel10 = new javax.swing.JLabel();
        CMND = new org.jdesktop.swingx.JXTextField();
        jLabel11 = new javax.swing.JLabel();
        LoaiSo = new javax.swing.JComboBox();

        setTitle("Tìm Kiếm Sổ Tiết Kiệm");
        setToolTipText("");

        Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(Table);

        jLabel2.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel2.setText("Mã Sổ");

        MaSo.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                MaSoCaretUpdate(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel3.setText("Họ Tên");

        HoTen.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                HoTenCaretUpdate(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel10.setText("CMND");

        CMND.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                CMNDCaretUpdate(evt);
            }
        });

        jLabel11.setFont(new java.awt.Font("Times New Roman", 0, 13)); // NOI18N
        jLabel11.setText("Loại Sổ");

        LoaiSo.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                LoaiSoItemStateChanged(evt);
            }
        });

        javax.swing.GroupLayout jXPanel1Layout = new javax.swing.GroupLayout(jXPanel1);
        jXPanel1.setLayout(jXPanel1Layout);
        jXPanel1Layout.setHorizontalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 960, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel2)
                        .addGap(30, 30, 30)
                        .addComponent(MaSo, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(41, 41, 41)
                        .addComponent(jLabel11, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(56, 56, 56)
                        .addComponent(jLabel3)
                        .addGap(30, 30, 30)
                        .addComponent(HoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(41, 41, 41)
                        .addComponent(jLabel10, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(43, 43, 43)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(LoaiSo, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CMND, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );
        jXPanel1Layout.setVerticalGroup(
            jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jXPanel1Layout.createSequentialGroup()
                .addGap(37, 37, 37)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(MaSo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LoaiSo, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel11))))
                .addGap(11, 11, 11)
                .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(HoTen, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CMND, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jXPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addGroup(jXPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel10))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jXPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(141, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jXPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void MaSoCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_MaSoCaretUpdate
        try {
            // TODO add your handling code here:
            String sql=savingbook_bll.SearchSavingBook(
                    !MaSo.getText().equals(""),MaSo.getText(), 
                    !HoTen.getText().equals(""),HoTen.getText(), 
                    !CMND.getText().equals(""), CMND.getText(),
                    true,LoaiSo.getSelectedItem().toString()
                    );
            ReloadTable(sql);
        } catch (Exception ex) {
            Logger.getLogger(SearchSavingBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_MaSoCaretUpdate

    private void HoTenCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_HoTenCaretUpdate
        try {
            // TODO add your handling code here:
            String sql=savingbook_bll.SearchSavingBook(
                    !MaSo.getText().equals(""),MaSo.getText(), 
                    !HoTen.getText().equals(""),HoTen.getText(), 
                    !CMND.getText().equals(""), CMND.getText(),
                    true,LoaiSo.getSelectedItem().toString()
                    );
            ReloadTable(sql);
        } catch (Exception ex) {
            Logger.getLogger(SearchSavingBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_HoTenCaretUpdate

    private void CMNDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_CMNDCaretUpdate
        try {
            // TODO add your handling code here:
            String sql=savingbook_bll.SearchSavingBook(
                    !MaSo.getText().equals(""),MaSo.getText(), 
                    !HoTen.getText().equals(""),HoTen.getText(), 
                    !CMND.getText().equals(""), CMND.getText(),
                    true,LoaiSo.getSelectedItem().toString()
                    );
           ReloadTable(sql);
        } catch (Exception ex) {
            Logger.getLogger(SearchSavingBook.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_CMNDCaretUpdate

    private void LoaiSoItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_LoaiSoItemStateChanged
        // TODO add your handling code here:
        if(evt.getStateChange()== ItemEvent.SELECTED)
        {
            try {
                String sql=savingbook_bll.SearchSavingBook(
                        !MaSo.getText().equals(""),MaSo.getText(), 
                        !HoTen.getText().equals(""),HoTen.getText(), 
                        !CMND.getText().equals(""), CMND.getText(),
                        true,evt.getItem().toString()
                        );
               ReloadTable(sql);
            } catch (Exception ex) {
                Logger.getLogger(SearchSavingBook.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }//GEN-LAST:event_LoaiSoItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXTextField CMND;
    private org.jdesktop.swingx.JXTextField HoTen;
    private javax.swing.JComboBox LoaiSo;
    private org.jdesktop.swingx.JXTextField MaSo;
    private org.jdesktop.swingx.JXTable Table;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private org.jdesktop.swingx.JXPanel jXPanel1;
    // End of variables declaration//GEN-END:variables
}
